package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.service.CourseService;
import com.lagou.service.impl.CourseServiceImpl;
import com.lagou.utils.DateUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/course")
public class CourseServlet extends BaseServlet {

    //查询课程信息列表
    public void findCourseList(HttpServletRequest request, HttpServletResponse response){

        try {
            //业务处理
            CourseService cs = new CourseServiceImpl();
            //得到查询的结果，即课程信息列表
            List<Course> courseList = cs.findCourseList();
            //响应结果
            //指定要转换的JSON字段
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id","course_name","price","sort_num","status");
            String result = JSON.toJSONString(courseList,filter);
            //将响应结果发送到浏览器
            response.getWriter().print(result);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据条件查询课程信息
    public void findByCourseNameAndStatus(HttpServletRequest request,HttpServletResponse response){

        try {
            //接收参数
            String course_name = request.getParameter("course_name");
            String status = request.getParameter("status");
            //业务处理
            CourseService cs = new CourseServiceImpl();
            List<Course> courseList = cs.findByCourseNameAndStatus(course_name, status);
            //返回结果
            //指定要转换的JSON字段
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id","course_name","price","sort_num","status");
            //转换为JSON
            String result = JSON.toJSONString(courseList, filter);
            //将响应结果发送到浏览器
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据课程id查询课程信息
    public void findCourseById(HttpServletRequest request,HttpServletResponse response){

        try {
            //接收参数
            int id = Integer.parseInt(request.getParameter("id"));
            //业务处理
            CourseService courseService = new CourseServiceImpl();
            //得到结果
            Course courseById = courseService.findCourseById(id);

            //指定要转换的JSON字段
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id",
                    "course_name","brief","teacher_name","teacher_info","price","price_tag",
                    "discounts","preview_first_field","preview_second_field","course_img_url",
                    "share_title","share_image_title","share_description","course_description","status");
            //转换成JSON
            String result = JSON.toJSONString(courseById,filter);
            //将响应结果发送到浏览器
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //修改课程状态
    public void updateCourseStatus(HttpServletRequest request, HttpServletResponse response){

        try {
            //获取参数
            String id = request.getParameter("id");

            //业务处理
            CourseService cs = new CourseServiceImpl();

            //查询课程信息
            Course courseById = cs.findCourseById(Integer.parseInt(id));

            //判断课程信息状态
            int status = courseById.getStatus();
            if(status == 0){
                //如状态为0，则修改为1
                courseById.setStatus(1);
            }else{
                //如为1，则修改为0
                courseById.setStatus(0);
            }

            //设置更新时间
            courseById.setUpdate_time(DateUtils.getDateFormart());

            //修改状态
            Map<String, Integer> map = cs.updateCourseStatus(courseById);

            //响应结果
            String s = JSON.toJSONString(map);
            //将响应结果发送到浏览器
            response.getWriter().print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
